<?xml version='1.0' encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <link href="cpip.css" rel="stylesheet" type="text/css" />
    <title>File: /Users/paulross/dev/linux/linux-3.13/arch/x86/include/asm/div64.h</title>
  </head>
  <body>
    <h1>File: /Users/paulross/dev/linux/linux-3.13/arch/x86/include/asm/div64.h</h1>
    <p>Green shading in the line number column
means the source is part of the translation unit, red means it is conditionally excluded.
Highlighted line numbers link to the translation unit page. Highlighted macros link to
the macro page.</p>
    <pre><a name="1" /><span class="Maybe">       1:</span> <span class="f">#</span><span class="n">ifndef</span> <a href="cpu.c_macros_ref.html#_X0FTTV9YODZfRElWNjRfSF8w"><span class="b">_ASM_X86_DIV64_H</span></a>
<a name="2" /><span class="Maybe">       2:</span> <span class="f">#</span><span class="n">define</span> <a href="cpu.c_macros_ref.html#_X0FTTV9YODZfRElWNjRfSF8w"><span class="b">_ASM_X86_DIV64_H</span></a>
<a name="3" /><span class="Maybe">       3:</span> 
<a name="4" /><span class="False">       4:</span> <span class="f">#</span><span class="n">ifdef</span> <span class="b">CONFIG_X86_32</span>
<a name="5" /><span class="False">       5:</span> 
<a name="6" /><span class="False">       6:</span> <span class="f">#</span><span class="n">include</span> <span class="f">&lt;</span><span class="b">linux</span><span class="f">/</span><span class="b">types</span><span class="f">.</span><span class="b">h</span><span class="f">&gt;</span>
<a name="7" /><span class="False">       7:</span> <span class="f">#</span><span class="n">include</span> <span class="f">&lt;</span><span class="b">linux</span><span class="f">/</span><span class="b">log2</span><span class="f">.</span><span class="b">h</span><span class="f">&gt;</span>
<a name="8" /><span class="False">       8:</span> 
<a name="9" /><span class="False">       9:</span> <span class="k">/*</span>
<a name="10" /><span class="False">      10:</span> <span class="k"> * do_div() is NOT a C function. It wants to return</span>
<a name="11" /><span class="False">      11:</span> <span class="k"> * two values (the quotient and the remainder), but</span>
<a name="12" /><span class="False">      12:</span> <span class="k"> * since that doesn&apos;t work very well in C, what it</span>
<a name="13" /><span class="False">      13:</span> <span class="k"> * does is:</span>
<a name="14" /><span class="False">      14:</span> <span class="k"> *</span>
<a name="15" /><span class="False">      15:</span> <span class="k"> * - modifies the 64-bit dividend _in_place_</span>
<a name="16" /><span class="False">      16:</span> <span class="k"> * - returns the 32-bit remainder</span>
<a name="17" /><span class="False">      17:</span> <span class="k"> *</span>
<a name="18" /><span class="False">      18:</span> <span class="k"> * This ends up being the most efficient &quot;calling</span>
<a name="19" /><span class="False">      19:</span> <span class="k"> * convention&quot; on x86.</span>
<a name="20" /><span class="False">      20:</span> <span class="k"> */</span>
<a name="21" /><span class="False">      21:</span> <span class="f">#</span><span class="n">define</span> <a href="cpu.c_macros_ref.html#_ZG9fZGl2XzA_"><span class="b">do_div</span></a><span class="f">(</span><span class="b">n</span><span class="f">,</span> <span class="b">base</span><span class="f">)</span>                        \
<a name="22" /><span class="False">      22:</span> <span class="f">(</span><span class="f">{</span>                                \
<a name="23" /><span class="False">      23:</span>     <span class="m">unsigned</span> <span class="m">long</span> <span class="b">__upper</span><span class="f">,</span> <span class="b">__low</span><span class="f">,</span> <span class="b">__high</span><span class="f">,</span> <span class="b">__mod</span><span class="f">,</span> <span class="b">__base</span><span class="f">;</span>    \
<a name="24" /><span class="False">      24:</span>     <span class="b">__base</span> <span class="f">=</span> <span class="f">(</span><span class="b">base</span><span class="f">)</span><span class="f">;</span>                    \
<a name="25" /><span class="False">      25:</span>     <span class="m">if</span> <span class="f">(</span><span class="b">__builtin_constant_p</span><span class="f">(</span><span class="b">__base</span><span class="f">)</span> <span class="f">&amp;&amp;</span> <span class="b">is_power_of_2</span><span class="f">(</span><span class="b">__base</span><span class="f">)</span><span class="f">)</span> <span class="f">{</span> \
<a name="26" /><span class="False">      26:</span>         <span class="b">__mod</span> <span class="f">=</span> <span class="b">n</span> <span class="f">&amp;</span> <span class="f">(</span><span class="b">__base</span> <span class="f">-</span> <span class="c">1</span><span class="f">)</span><span class="f">;</span>            \
<a name="27" /><span class="False">      27:</span>         <span class="b">n</span> <span class="f">&gt;&gt;=</span> <a href="cpu.c_macros_ref.html#_aWxvZzJfMA__"><span class="b">ilog2</span></a><span class="f">(</span><span class="b">__base</span><span class="f">)</span><span class="f">;</span>                \
<a name="28" /><span class="False">      28:</span>     <span class="f">}</span> <span class="m">else</span> <span class="f">{</span>                        \
<a name="29" /><span class="False">      29:</span>         <span class="m">asm</span><span class="f">(</span><span class="e">&quot;&quot;</span> <span class="f">:</span> <span class="e">&quot;=a&quot;</span> <span class="f">(</span><span class="b">__low</span><span class="f">)</span><span class="f">,</span> <span class="e">&quot;=d&quot;</span> <span class="f">(</span><span class="b">__high</span><span class="f">)</span> <span class="f">:</span> <span class="e">&quot;A&quot;</span> <span class="f">(</span><span class="b">n</span><span class="f">)</span><span class="f">)</span><span class="f">;\
</span>        <span class="b">__upper</span> <span class="f">=</span> <span class="b">__high</span><span class="f">;</span>                \
<a name="31" /><span class="False">      31:</span>         <span class="m">if</span> <span class="f">(</span><span class="b">__high</span><span class="f">)</span> <span class="f">{</span>                    \
<a name="32" /><span class="False">      32:</span>             <span class="b">__upper</span> <span class="f">=</span> <span class="b">__high</span> <span class="f">%</span> <span class="f">(</span><span class="b">__base</span><span class="f">)</span><span class="f">;</span>        \
<a name="33" /><span class="False">      33:</span>             <span class="b">__high</span> <span class="f">=</span> <span class="b">__high</span> <span class="f">/</span> <span class="f">(</span><span class="b">__base</span><span class="f">)</span><span class="f">;</span>        \
<a name="34" /><span class="False">      34:</span>         <span class="f">}</span>                        \
<a name="35" /><span class="False">      35:</span>         <span class="m">asm</span><span class="f">(</span><span class="e">&quot;divl %2&quot;</span> <span class="f">:</span> <span class="e">&quot;=a&quot;</span> <span class="f">(</span><span class="b">__low</span><span class="f">)</span><span class="f">,</span> <span class="e">&quot;=d&quot;</span> <span class="f">(</span><span class="b">__mod</span><span class="f">)</span>    \
<a name="36" /><span class="False">      36:</span>             <span class="f">:</span> <span class="e">&quot;rm&quot;</span> <span class="f">(</span><span class="b">__base</span><span class="f">)</span><span class="f">,</span> <span class="e">&quot;0&quot;</span> <span class="f">(</span><span class="b">__low</span><span class="f">)</span><span class="f">,</span> <span class="e">&quot;1&quot;</span> <span class="f">(</span><span class="b">__upper</span><span class="f">)</span><span class="f">)</span><span class="f">;</span>    \
<a name="37" /><span class="False">      37:</span>         <span class="m">asm</span><span class="f">(</span><span class="e">&quot;&quot;</span> <span class="f">:</span> <span class="e">&quot;=A&quot;</span> <span class="f">(</span><span class="b">n</span><span class="f">)</span> <span class="f">:</span> <span class="e">&quot;a&quot;</span> <span class="f">(</span><span class="b">__low</span><span class="f">)</span><span class="f">,</span> <span class="e">&quot;d&quot;</span> <span class="f">(</span><span class="b">__high</span><span class="f">)</span><span class="f">)</span><span class="f">;</span>    \
<a name="38" /><span class="False">      38:</span>     <span class="f">}</span>                            \
<a name="39" /><span class="False">      39:</span>     <span class="b">__mod</span><span class="f">;</span>                            \
<a name="40" /><span class="False">      40:</span> <span class="f">}</span><span class="f">)</span>
<a name="41" /><span class="False">      41:</span> 
<a name="42" /><span class="False">      42:</span> <span class="m">static</span> <span class="m">inline</span> <span class="b">u64</span> <span class="b">div_u64_rem</span><span class="f">(</span><span class="b">u64</span> <span class="b">dividend</span><span class="f">,</span> <a href="cpu.c_macros_ref.html#_dTMyXzA_"><span class="b">u32</span></a> <span class="b">divisor</span><span class="f">,</span> <a href="cpu.c_macros_ref.html#_dTMyXzA_"><span class="b">u32</span></a> <span class="f">*</span><span class="b">remainder</span><span class="f">)</span>
<a name="43" /><span class="False">      43:</span> <span class="f">{</span>
<a name="44" /><span class="False">      44:</span>     <span class="m">union</span> <span class="f">{</span>
<a name="45" /><span class="False">      45:</span>         <span class="b">u64</span> <span class="b">v64</span><span class="f">;</span>
<a name="46" /><span class="False">      46:</span>         <a href="cpu.c_macros_ref.html#_dTMyXzA_"><span class="b">u32</span></a> <span class="b">v32</span><span class="f">[</span><span class="c">2</span><span class="f">]</span><span class="f">;</span>
<a name="47" /><span class="False">      47:</span>     <span class="f">}</span> <span class="b">d</span> <span class="f">=</span> <span class="f">{</span> <span class="b">dividend</span> <span class="f">}</span><span class="f">;</span>
<a name="48" /><span class="False">      48:</span>     <a href="cpu.c_macros_ref.html#_dTMyXzA_"><span class="b">u32</span></a> <span class="b">upper</span><span class="f">;</span>
<a name="49" /><span class="False">      49:</span> 
<a name="50" /><span class="False">      50:</span>     <span class="b">upper</span> <span class="f">=</span> <span class="b">d</span><span class="f">.</span><span class="b">v32</span><span class="f">[</span><span class="c">1</span><span class="f">]</span><span class="f">;</span>
<a name="51" /><span class="False">      51:</span>     <span class="b">d</span><span class="f">.</span><span class="b">v32</span><span class="f">[</span><span class="c">1</span><span class="f">]</span> <span class="f">=</span> <span class="c">0</span><span class="f">;</span>
<a name="52" /><span class="False">      52:</span>     <span class="m">if</span> <span class="f">(</span><span class="b">upper</span> <span class="f">&gt;=</span> <span class="b">divisor</span><span class="f">)</span> <span class="f">{</span>
<a name="53" /><span class="False">      53:</span>         <span class="b">d</span><span class="f">.</span><span class="b">v32</span><span class="f">[</span><span class="c">1</span><span class="f">]</span> <span class="f">=</span> <span class="b">upper</span> <span class="f">/</span> <span class="b">divisor</span><span class="f">;</span>
<a name="54" /><span class="False">      54:</span>         <span class="b">upper</span> <span class="f">%=</span> <span class="b">divisor</span><span class="f">;</span>
<a name="55" /><span class="False">      55:</span>     <span class="f">}</span>
<a name="56" /><span class="False">      56:</span>     <span class="m">asm</span> <span class="f">(</span><span class="e">&quot;divl %2&quot;</span> <span class="f">:</span> <span class="e">&quot;=a&quot;</span> <span class="f">(</span><span class="b">d</span><span class="f">.</span><span class="b">v32</span><span class="f">[</span><span class="c">0</span><span class="f">]</span><span class="f">)</span><span class="f">,</span> <span class="e">&quot;=d&quot;</span> <span class="f">(</span><span class="f">*</span><span class="b">remainder</span><span class="f">)</span> <span class="f">:</span>
<a name="57" /><span class="False">      57:</span>         <span class="e">&quot;rm&quot;</span> <span class="f">(</span><span class="b">divisor</span><span class="f">)</span><span class="f">,</span> <span class="e">&quot;0&quot;</span> <span class="f">(</span><span class="b">d</span><span class="f">.</span><span class="b">v32</span><span class="f">[</span><span class="c">0</span><span class="f">]</span><span class="f">)</span><span class="f">,</span> <span class="e">&quot;1&quot;</span> <span class="f">(</span><span class="b">upper</span><span class="f">)</span><span class="f">)</span><span class="f">;</span>
<a name="58" /><span class="False">      58:</span>     <span class="m">return</span> <span class="b">d</span><span class="f">.</span><span class="b">v64</span><span class="f">;</span>
<a name="59" /><span class="False">      59:</span> <span class="f">}</span>
<a name="60" /><span class="False">      60:</span> <span class="f">#</span><span class="n">define</span> <span class="b">div_u64_rem</span>    <span class="b">div_u64_rem</span>
<a name="61" /><span class="False">      61:</span> 
<a name="62" /><span class="Maybe">      62:</span> <span class="f">#</span><span class="n">else</span>
<a name="63" /><span class="Maybe">      63:</span> <span class="f">#</span> <span class="n">include</span> <span class="f">&lt;</span><span class="m">asm</span><span class="f">-</span><span class="b">generic</span><span class="f">/</span><span class="b">div64</span><span class="f">.</span><span class="b">h</span><span class="f">&gt;</span>
<a name="64" /><span class="Maybe">      64:</span> <span class="f">#</span><span class="n">endif</span> <span class="k">/* CONFIG_X86_32 */</span>
<a name="65" /><span class="Maybe">      65:</span> 
<a name="66" /><span class="True">      66:</span> <span class="f">#</span><span class="n">endif</span> <span class="k">/* _ASM_X86_DIV64_H */</span>
<a name="67" /><span class="True">      67:</span> </pre>
  </body>
</html>
